<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
    >
      <el-form-item label="订单号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单状态" prop="orderState">
        <el-select
          v-model="queryParams.orderState"
          @change="handleQuery"
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in orderstatusoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="仓库数据推送状态"
        prop="warehouseState"
        label-width="150px"
      >
        <el-select
          v-model="queryParams.warehouseState"
          @change="handleQuery"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in Stateoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="微信支付单推送状态"
        prop="afterstatus"
        label-width="150px"
      >
        <el-select
          v-model="queryParams.wechatState"
          @change="handleQuery"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in Stateoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="海关状态" prop="ddhdzt" label-width="100px">
        <el-select
          v-model="queryParams.customsState"
          @change="handleQuery"
          clearable
          placeholder="请选择"
        >
          <el-option
            v-for="item in Stateoptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单时间" prop="ordertime">
        <el-date-picker
          v-model="queryParams.payTime"
          type="datetime"
          value-format="yyyy-MM-DD hh:mm:ss"
          placeholder="请输入下单时间"
          @change="handleQuery"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table ref="multipleTable" v-loading="loading" :data="order">
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="序号" align="center" prop="id" width="100">
        <template slot-scope="scope">
          <div>
            {{
              scope.$index +
              (queryParams.pageNum - 1) * queryParams.pageSize +
              1
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="订单号"
        align="center"
        prop="orderCode"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="总金额"
        align="center"
        prop="moneyTotal"
        width="100"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.moneyTotal ? scope.row.moneyTotal : "0" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="仓库数据推送状态"
        align="center"
        prop="warehouseState"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.warehouseState == "1" ? "未推送" : "推送成功" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="微信支付单推送状态"
        align="center"
        prop="wechatState"
        width="150"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.warehouseState == "1" ? "未推送" : "推送成功" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="海关状态"
        align="center"
        prop="customsState"
        width="100"
      >
        <template slot-scope="scope">
          <div>
            {{ scope.row.warehouseState == "1" ? "未推送" : "推送成功" }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="实际金额"
        align="center"
        prop="moneyReal"
        width="100"
      >
      </el-table-column>
      <el-table-column
        label="收货人地址"
        align="center"
        prop="receiveArea"
        width="200"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="收件人"
        align="center"
        prop="receiveName"
        width="100"
      >
      </el-table-column>
      <el-table-column
        label="下单时间"
        align="center"
        prop="createTime"
        width="100"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        align="center"
        prop="opration"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="mini"
            icon="el-icon-document"
            @click="handleedit('edit', scope.row)"
            >明细</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改公告对话框 -->
    <el-dialog
      title="订单列表"
      :visible="open"
      width="90%"
      append-to-body
      :before-close="handeclose"
    >
      <el-button type="primary" @click="handleDown">PDF下载</el-button>
      <div id="demo">
        <table border="1" class="table_style">
          <thead>
            <th colspan="5" style="font-size: 10px; color: red" align="left">
              订单信息
            </th>
          </thead>
          <tbody>
            <tr>
              <td>订单号</td>
              <td>状态</td>
              <td>订单金额</td>
              <td>税费</td>
              <td>总重</td>
            </tr>
            <tr>
              <td>{{ ordermsg.orderCode }}</td>
              <td>{{ ordermsg.orderState }}</td>
              <td>{{ ordermsg.moneyReal }}</td>
              <td>{{ ordermsg.moneyTax ? ordermsg.moneyTax * 100 : 0 }}%</td>
              <td>{{ ordermsg.weight || "0.00" }}</td>
            </tr>
          </tbody>
        </table>
        <table border="1" class="table_style" style="margin-top: 10px">
          <thead>
            <th colspan="8" style="font-size: 10px; color: red" align="left">
              收货人物流信息
            </th>
          </thead>
          <tbody>
            <tr>
              <td>收货人姓名</td>
              <td>收货人电话</td>
              <td>收货人身份证号</td>
              <td>收货地址</td>
              <td>物流公司编号</td>
              <td>物流公司</td>
              <td>物流单号</td>
              <td>发货时间</td>
            </tr>
            <tr>
              <td>{{ logisticsmsg.receiveName }}</td>
              <td>{{ logisticsmsg.receivePhone }}</td>
              <td>{{ logisticsmsg.receiveIdcard }}</td>
              <td style="min-width: 300px">
                {{ logisticsmsg.receiveAddress }}
              </td>
              <td>{{ logisticsmsg.expressCode }}</td>
              <td style="max-width: 120px">{{ logisticsmsg.expressName }}</td>
              <td>{{ logisticsmsg.expressOrderSn }}</td>
              <td>{{ logisticsmsg.shipmentsTime }}</td>
            </tr>
          </tbody>
        </table>
        <table border="1" class="table_style" style="margin-top: 10px">
          <thead>
            <th colspan="6" style="font-size: 10px; color: red" align="left">
              支付信息
            </th>
          </thead>
          <tbody>
            <tr>
              <td>支付方式</td>
              <td>支付流水号</td>
              <td>支付时间</td>
              <td>真实姓名</td>
              <td>身份证号</td>
              <td>手机号</td>
            </tr>
            <tr>
              <td>{{ payformsg.payType }}</td>
              <td>{{ payformsg.payOrderSn }}</td>
              <td>{{ payformsg.payTime }}</td>
              <td></td>
              <td></td>
              <td></td>
            </tr>
          </tbody>
        </table>
        <table border="1" class="table_style" style="margin-top: 10px">
          <thead>
            <th colspan="8" style="font-size: 10px; color: red" align="left">
              订单商品详情
            </th>
          </thead>
          <tbody>
            <tr>
              <td>商品名称</td>
              <td>商品编码</td>
              <td>规格编码</td>
              <td>海关编码</td>
              <td>原产国</td>
              <td>购买数量</td>
              <td>重量</td>
              <td>价格</td>
            </tr>
            <tr v-for="(item, index) in orderproduct" :key="index">
              <td style="max-width: 300px">{{ item.commodityName }}</td>
              <td>{{ item.commodityBaseCode }}</td>
              <td>{{ item.specificationBaseCode }}</td>
              <td>{{ item.skuCode }}</td>
              <td>{{ item.countryOfOrigin }}</td>
              <td>{{ item.amount }}</td>
              <td>{{ item.weight || "0.00" }}</td>
              <td>{{ item.price || "0.00" }}</td>
            </tr>
          </tbody>
        </table>
        <div style="margin-top: 10px">
          <span style="font-size: 10px; color: red; font-weight: 700"
            >备注</span
          >
          <div class="remark">{{ remark }}</div>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listorder, listorderdetial } from "@/api/order";
import Editor from "@/components/Editor";
import selfDirective from "@/utils/selfDirective";
import FileUpload from "@/components/FileUpload/index";
import htmlToPdf from "@/utils/htmlToPdf.js";

export default {
  name: "commodity",
  components: {
    Editor,
    FileUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公告表格数据
      order: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 渠道名称
      qudaooptions: [],
      // 仓订单回调状态
      ddhdztoptions: [],
      //订单状态
      orderstatusoptions: [
        {
          value: "1",
          label: "待付款",
        },
        {
          value: "2",
          label: "待发货",
        },
        {
          value: "3",
          label: "待收货",
        },
        {
          value: "4",
          label: "退换货",
        },
        {
          value: "5",
          label: "已完成",
        },
        {
          value: "6",
          label: "退换成功",
        },
        {
          value: "7",
          label: "已关闭",
        },
      ],
      payforoptions: [
        {
          value: "11",
          label: "支付宝",
        },
        {
          value: "20",
          label: "微信支付",
        },
        {
          value: "21",
          label: "微信公众号网页支付",
        },
        {
          value: "40",
          label: "我的钱包支付",
        },
      ],
      Stateoptions: [
        {
          value: "-1",
          label: "失败",
        },
        {
          value: "0",
          label: "未推送",
        },
        {
          value: "1",
          label: "推送成功",
        },
      ],
      afterstatusoptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        icon: [],
        sort: null,
        name: "",
      },
      // 表单校验
      rules: {
        gsskzhname: [
          { required: true, message: "公司名称不能为空", trigger: "change" },
        ],
        gsskshh: [
          {
            required: true,
            message: "公司收款商户号不能为空",
            trigger: "change",
          },
        ],
        gsskyhmc: [
          {
            required: true,
            message: "公司收款银行名称不能为空",
            trigger: "change",
          },
        ],
        gsskzh: [
          {
            required: true,
            message: "公司收款账户不能为空",
            trigger: "change",
          },
        ],
      },
      type: "0",
      multipleSelection: [],
      limit: 5,
      fileSize: 20,
      isShowTip: false,
      value: [],
      gsskshhoptions: [],
      producttypeoptions: [],
      gsskzhoptions: [],
      ordertableData: [],
      leaveData: {
        dimId: "",
      },
      approvalLogs: [],
      ordermsg: {
        orderCode: "",
        orderState: "",
        moneyReal: "",
        moneyTax: "",
        weight: "",
      },
      logisticsmsg: {
        receiveName: "",
        receivePhone: "",
        receiveIdcard: "",
        receiveAddress: "",
        expressCode: "",
        expressName: "",
        expressOrderSn: "",
        shipmentsTime: "",
      },
      payformsg: {
        payType: "",
        payOrderSn: "",
        payTime: "",
      },
      orderproduct: [],
      remark: "",
      conutryarr: [],
    };
  },
  created() {
    this.getList();
    this.getDicts("gj").then((response) => {
      this.conutryarr = response.data;
    });
  },
  methods: {
    chooseproduct() {
      if (this.multipleSelection.length == 0) {
        this.$message.error("请选择一条商品数据");
        return;
      }
      chooseproduct(this.multipleSelection).then((res) => {
        this.msgSuccess("选品成功");
        this.multipleSelection = [];
        this.back();
      });
    },
    handleSelectionChange(val) {
      console.log(val);
      this.multipleSelection = val;
    },
    /** 查询公告列表 */
    handelcopy() {
      copycommodity().then((res) => {
        this.msgSuccess("复制成功");
      });
    },
    /** 查询公告列表 */
    changeStatus(value) {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 查询公告列表 */
    getList() {
      this.loading = true;
      listorderdetial(this.queryParams).then((response) => {
        var order = response.rows;
        this.order = order;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 公告状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 公告状态字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.noticeType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            updatecommodity(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addcommodity(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    handleedit(type, row) {
      this.open = true;
      this.ordermsg = {
        orderCode: row.orderCode,
        orderState: this.getdictdata(
          row.orderState,
          this.orderstatusoptions,
          "value",
          "label"
        ),
        moneyReal: row.moneyReal,
        moneyTax: row.moneyTax,
        weight: row.weight,
      };
      let receiveIdcard = row.receiveIdcard;
      let receiveIdcardstr = receiveIdcard
        ? receiveIdcard.slice(0, 3) + "******" + receiveIdcard.substr(-3, 3)
        : "";
      this.logisticsmsg = {
        receiveName: row.receiveName,
        receivePhone: row.receivePhone,
        receiveIdcard: receiveIdcardstr,
        receiveAddress: row.receiveAddress,
        expressCode: row.expressCode,
        expressName: row.expressName,
        expressOrderSn: row.expressOrderSn,
        shipmentsTime: row.shipmentsTime,
      };
      this.payformsg = {
        payType: this.getdictdata(
          row.payType,
          this.payforoptions,
          "value",
          "label"
        ),
        payOrderSn: row.payOrderSn,
        payTime: row.payTime,
      };
      let detailList = row.detailList;
      detailList.map((item) => {
        item.countryOfOrigin = this.getconutry(item.countryOfOrigin);
      });
      this.orderproduct = detailList;
      this.remark = row.remark;
    },
    getconutry(conutry) {
      let conutryarr = this.conutryarr;
      let conutrystr = "";
      conutryarr.map((item) => {
        if (item.dictValue == conutry) {
          conutrystr = item.dictLabel;
        }
      });
      return conutrystr;
    },
    getdictdata(value, arr, arritem, getitme) {
      var str = "";
      arr.map((item) => {
        if (item[arritem] == value) {
          str = item[getitme];
        }
      });
      return str;
    },
    handeclose() {
      this.open = false;
    },
    handleDown() {
      htmlToPdf.downloadPDF(document.querySelector("#demo"), "订单详情");
    },
  },
};
</script>
<style scoped lang="scss">
.radio-box {
  margin-bottom: 10px;
}
.stock {
  text-decoration: underline;
}
.copy {
  color: #999;
  cursor: pointer;
}
.box-card {
  margin-top: 20px;
  .card-title {
    font-weight: 700;
    font-style: normal;
    color: #555555;
  }
  .baseinfo {
    width: 100%;
    height: 40px;
    line-height: 40px;
    color: #999;
    background-color: #f2f2f2;
    padding-left: 10px;
    border-radius: 5px;
    margin-top: 10px;
  }
}
#demo {
  background-color: #fff;
  width: 100%;
  /* height: 400px; */
  margin: auto;
  padding: 40px;
  box-sizing: border-box;
}

.table_style td,
th {
  padding: 10px;
  font-size: 10px;
  font-weight: 700;
}

.table_style {
  border-collapse: collapse;
  width: 100%;
  text-align: center;
}
.remark {
  border: 1px solid #606266;
  min-height: 50px;
  margin-top: 10px;
  padding: 5px;
  box-sizing: border-box;
}
</style>